home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Examples / CFMExamples / ModApp / Init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-03  |  4.0 KB  |  199 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Init.c
  3.  
  4.     Contains:    Initialization code for this application
  5.                 (also includes code to check the system configuration and
  6.                 look for externally loaded libraries)
  7.  
  8.     Written by:    Richard Clark
  9.  
  10.     Copyright:    © 1993 by Apple Computer, Inc., all rights reserved.
  11.  
  12.     Change History (most recent first):
  13.  
  14.                   8/15/94    BLS        Updated includes per new universal headers.
  15.                   12/3/93    RC        Added code to look for optional libraries
  16.                                       and build pointers to their exported functions
  17.  
  18.                   11/28/93    RC        First release
  19.  
  20.     To Do:
  21. */
  22.  
  23.  
  24. #ifndef __DIALOGS__
  25.     #include <Dialogs.h>
  26. #endif
  27.  
  28. #ifndef __FONTS__
  29.     #include <Fonts.h>
  30. #endif
  31.  
  32. #ifndef __WINDOWS__
  33.     #include <Windows.h>
  34. #endif
  35.  
  36. #ifndef __MENUS__
  37.     #include <Menus.h>
  38. #endif
  39.  
  40. #ifndef __TEXTEDIT__
  41.     #include <TextEdit.h>
  42. #endif
  43.  
  44. #ifndef __EVENTS__
  45.     #include <Events.h>
  46. #endif
  47.  
  48. #ifndef __OSEVENTS__
  49.     #include <Events.h>
  50. #endif
  51.  
  52. #ifndef __GESTALT__
  53.     #include <Gestalt.h>
  54. #endif
  55.  
  56. #ifndef __FRAGLOAD__
  57.     #include <CodeFragments.h>
  58. #endif
  59.  
  60. #ifndef __MIXEDMODE__
  61.     #include <MixedMode.h>
  62. #endif
  63.  
  64. #ifndef __PROCESSES__
  65.     #include <Processes.h>
  66. #endif
  67.  
  68. #ifndef __TRAPS__
  69.     #include <Traps.h>
  70. #endif
  71.  
  72. #include "ModApp.h"
  73. #include "Prototypes.h"
  74.  
  75. // === Local prototypes & #defines
  76. static short NumToolboxTraps ( void );
  77. static TrapType GetTrapType (short theTrap);
  78. static Boolean TrapAvailable (short theTrap);
  79.  
  80. #define TrapMask 0x0800
  81.  
  82. // === Local functions
  83.  
  84. static short NumToolboxTraps( void )
  85. {
  86.     if (NGetTrapAddress(_InitGraf, ToolTrap) ==
  87.             NGetTrapAddress(0xAA6E, ToolTrap))
  88.         return(0x0200);
  89.     else
  90.         return(0x0400);
  91. }
  92.  
  93. static TrapType GetTrapType(short theTrap)
  94. {
  95.  
  96.     if ((theTrap & TrapMask) > 0)
  97.         return(ToolTrap);
  98.     else
  99.         return(OSTrap);
  100.  
  101. }
  102.  
  103. static Boolean TrapAvailable(short theTrap)
  104. {
  105.  
  106.     TrapType    tType;
  107.  
  108.     tType = GetTrapType(theTrap);
  109.     if (tType == ToolTrap)
  110.     theTrap = theTrap & 0x07FF;
  111.     if (theTrap >= NumToolboxTraps())
  112.         theTrap = _Unimplemented;
  113.  
  114.     return (NGetTrapAddress(theTrap, tType) !=
  115.             NGetTrapAddress(_Unimplemented, ToolTrap));
  116. }
  117.  
  118. Boolean LibAvailable(Str255 libName)
  119. // Check to see if a particular shared library is available
  120. {
  121.     CFragConnectionID    connID;
  122.     Ptr                    mainAddr;
  123.     Str255                errName;
  124.     OSErr                err;
  125.     
  126.     if (!gHasCFM) {
  127.         return false;    // No Code Fragment Manager, hence no shared library
  128.     } else {
  129.         err = GetSharedLibrary(libName, kAnyCFragArch, kFindCFrag, &connID, &mainAddr, errName);
  130.         return (err == noErr);
  131.     }
  132. }
  133.  
  134.  
  135. static Boolean CheckConfiguration ()
  136. {
  137.     long        result;
  138.     OSErr        err;
  139.     Boolean        hasAppleEvents, hasFSpTraps, hasNewStdFile;
  140.     
  141.     // Verify that we can run on the current configuration
  142.     // We need:
  143.     //    Apple events
  144.     //    FSSpec-based Standard File
  145.     //    FSSpec-based file traps
  146.     //
  147.     // We would like to have:
  148.     //    Mixed Mode
  149.     //    Code Fragment Manager
  150.  
  151.     err = Gestalt(gestaltAppleEventsAttr, &result);
  152.     hasAppleEvents = ((err == noErr) && (result & (1 << gestaltAppleEventsPresent)));
  153.     
  154.     err = Gestalt(gestaltFSAttr, &result);
  155.     hasFSpTraps = ((err == noErr) && (result & (1 << gestaltHasFSSpecCalls)));
  156.     
  157.     err = Gestalt(gestaltStandardFileAttr, &result);
  158.     hasNewStdFile = ((err == noErr) && (result & (1 << gestaltStandardFile58)));
  159.         
  160.     // *** Start checking for optional system features that we would like
  161.     gHasMixedMode = TrapAvailable(_MixedModeMagic);
  162.     gHasCFM = TrapAvailable (_CodeFragmentDispatch);
  163.  
  164.     return (hasAppleEvents & hasFSpTraps & hasNewStdFile);
  165. }
  166.  
  167.  
  168. // === Exported functions
  169. void InitAll()
  170. {    
  171.     InitGraf(&qd.thePort);
  172.     InitFonts();
  173.     InitWindows();
  174.     InitMenus();
  175.     TEInit();
  176.     InitDialogs(NULL);
  177.  
  178.     FlushEvents(everyEvent, 0);
  179.  
  180.     gCommonMenuBar = GetNewMBar(1001);
  181.     SetMenuBar(gCommonMenuBar);    
  182.     AppendResMenu(GetMenuHandle(kAppleMenu), 'DRVR');
  183.     InitToolLoader();
  184.     BuildToolsMenu();    // Get a list of installed tools
  185.     DrawMenuBar();
  186.  
  187.     if (!CheckConfiguration()) {
  188.         AlertUser(kNeedSystem7, 0);
  189.         ExitToShell();
  190.     }
  191.  
  192.     gDone = false;                          /* initialize flag to control Main Event Loop    */
  193.     gMenuState = 0;                        /* We just loaded the menu bar */
  194.     gCurrentWindow = NULL;                /* No windows in front */
  195.     InstallAppleEventHandlers();
  196.     InitCursor();                          /* set the cursor to arrow instead of clock    */
  197.  
  198. }
  199.